package gbench.sandbox.zk.zoo;

import java.util.Map;

import org.apache.curator.framework.recipes.cache.ChildData;
import com.fasterxml.jackson.databind.ObjectMapper;

import gbench.common.tree.LittleTree.IRecord;
import gbench.common.tree.LittleTree.Term;

/**
 * 
 * @author Administrator
 *
 */
public class ZKNode {

    /**
     * 
     * @param node
     */
    public ZKNode(ChildData node) {
        super();
        this.node = node;
    }

    public ChildData getNode() {
        return node;
    }

    public void setNode(ChildData node) {
        this.node = node;
    }

    /**
     * 
     * @return
     */
    public IRecord getRecord() {
        final var json = new String(node.getData());
        final var objM = new ObjectMapper();
        Map<?, ?> map = null;
        try {
            System.out.println(json);
            map = (Map<?, ?>) objM.readValue(json, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return IRecord.REC(map);
    }

    /**
     * 
     * @return
     */
    public String path() {
        return this.node.getPath();
    }

    /**
     * 
     */
    public String toString() {
        final var line = Term.FT("【$0 => $1】", this.node.getPath(), this.getRecord());
        return line;
    }

    private ChildData node;
}
